package com.thinksns.sociax.t4.android.verify;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.android.widget.wheelview.AbstractWheelTextAdapter;
import com.thinksns.sociax.t4.android.widget.wheelview.OnWheelChangedListener;
import com.thinksns.sociax.t4.android.widget.wheelview.OnWheelClickedListener;
import com.thinksns.sociax.t4.android.widget.wheelview.WheelView;
import com.thinksns.sociax.t4.model.VerifyModel;
import com.thinksns.sociax.t4.unit.UnitSociax;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author LeiYan
 * @describe
 * @date 2017/7/24
 * @contact email:907238552@qq.com
 */

public class VerifyTypePopupWindow extends PopupWindow implements PopupWindow.OnDismissListener {

    private Context mContext;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;
    @BindView(R.id.wheel_view)
    WheelView mWheelView;
    private List<VerifyModel> mData;
    private View.OnClickListener mOnClickListener;

    public VerifyTypePopupWindow(Context context, List<VerifyModel> data) {
        super(context);
        this.mContext = context;
        this.mData = data;
        initView();
        initPop();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.popup_verify_type, null);
        ButterKnife.bind(this, view);
        setContentView(view);
        final AbstractWheelTextAdapter adapter = new AbstractWheelTextAdapter(mContext) {
            @Override
            protected CharSequence getItemText(int index) {
                return mData.get(index).getTitle();
            }

            @Override
            public int getItemsCount() {
                return mData == null ? 0 : mData.size();
            }

            @Override
            protected void configureTextView(TextView view) {
                view.setGravity(Gravity.CENTER);
                view.setMinHeight(UnitSociax.dip2px(view.getContext(), 40));
            }
        };
        mWheelView.setViewAdapter(adapter);
        mWheelView.setCurrentItem(0);
        mWheelView.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                if (newValue > mData.size() - 1) {
                    newValue = mData.size() - 1;
                }
                adapter.setCurrentIndex(newValue);
                mWheelView.setCurrentItem(newValue);
            }
        });
        mWheelView.addClickingListener(new OnWheelClickedListener() {
            @Override
            public void onItemClicked(WheelView wheel, int itemIndex) {
                if (mOnClickListener != null) {
                    wheel.setTag(mData.get(itemIndex));
                    mOnClickListener.onClick(wheel);
                }
            }
        });
    }

    private void initPop() {
        int height = UnitSociax.getWindowHeight(mContext) / 3;
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(height);
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable());
        setOnDismissListener(this);
    }

    @OnClick({R.id.tv_confirm, R.id.tv_cancel})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                int pos = mWheelView.getCurrentItem();
                view.setTag(mData.get(pos));
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(view);
                }
                dismiss();
                break;
        }
    }

    public void show(View parent, int gravity) {
        com.thinksns.sociax.thinksnsbase.utils.UnitSociax.setAlpha(mContext, 0.7f);
        showAtLocation(parent, gravity, 0, 0);
    }

    @Override
    public void onDismiss() {
        com.thinksns.sociax.thinksnsbase.utils.UnitSociax.setAlpha(mContext, 1f);
    }
}
